package aiproject3.util;

/**
 * A simple command-line argument parser.
 * 
 * @author John Schaeffer
 *
 */
public class ArgParser {

    private String[] args;
    
    /**
     * Builds an ArgParser.
     * 
     * @param args The arguments to be parsed.
     */
    public ArgParser(String[] args) {
        this.args = args;
    }
    /**
     * Determines whether or not a given variable exists in the argument environment.
     * 
     * @param s The variable to be found.
     * @return True if the corresponding variable has been found, false otherwise.
     */
    public boolean argExists(String s) {
        
        for(int i = 0; i < args.length; i++) {
            // Grab the variable name.
            String temp = args[i].substring(0,s.length());
            
            // If the variable has been found, return true.
            if(temp.equals(s)) return true;
        }
        
        // We didn't find anything--terminate.
        return false;
    }
    
    /**
     * Parses the command-line argument found in the list of command-line arguments.
     * 
     * @param s The argument to be parsed.
     * @param idx The index of the argument in the argument array.
     * @return The value of s, parsed as an integer.
     */
    public int parseArg(String s, int idx) {
        
        int rtn = Integer.parseInt(args[idx].substring(s.length()+1));
        return rtn;
    }
    
    /**
     * Gets the index of the command-line argument found in the list of command-line
     * arguments.
     * 
     * @param s The argument to be found.
     */
    public int argIdx(String s) {
        
        for(int i = 0; i < args.length; i++) {
            // Grab the variable name.
            String temp = args[i].substring(0,s.length());
            
            // If the variable has been found, return the index.
            if(temp.equals(s)) return i;
        }
        
        // We didn't find anything--terminate.
        return -1;
        
    }
}
